#!/usr/bin/python -S
"""
lock_demo.py
"""

__author__ = 'Andy Chu'


import sys
import fcntl
import time


class Error(Exception):
  pass


def main(argv):
  """Returns an exit code."""

  FILE = 'lock'

  # This could just be 'r' if we pre-create the lock file.
  print 'Opening'
  f = open(FILE, "w")

  print 'Waiting for lock'
  fcntl.flock(f.fileno(), fcntl.LOCK_EX)

  print 'Sleeping'
  # Just hold the lock
  time.sleep(int(argv[1]))
  return 0


if __name__ == '__main__':
  try:
    sys.exit(main(sys.argv))
  except Error, e:
    print >> sys.stderr, e.args[0]
    sys.exit(1)
